package com.example.xiangqigame

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            XiangqiTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ChessBoard()
                }
            }
        }
    }
}

@Composable
fun XiangqiTheme(content: @Composable () -> Unit) {
    MaterialTheme(
        colorScheme = MaterialTheme.colorScheme,
        content = content
    )
}

@Composable
fun ChessBoard() {
    var selectedPiece by remember { mutableStateOf<Pair<Int, Int>?>(null) }
    var board by remember { mutableStateOf(initChessBoard()) }

    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        for (y in 0 until 10) {
            Row {
                for (x in 0 until 9) {
                    val piece = board[y][x]
                    val isSelected = selectedPiece == Pair(x, y)
                    Box(
                        modifier = Modifier
                            .size(40.dp)
                            .background(
                                color = if ((x + y) % 2 == 0) Color.LightGray else Color.DarkGray
                            )
                            .clickable {
                                if (selectedPiece == null) {
                                    if (piece.isNotEmpty()) selectedPiece = Pair(x, y)
                                } else {
                                    board = movePiece(board, selectedPiece!!, Pair(x, y))
                                    selectedPiece = null
                                }
                            }
                            .border(
                                width = 2.dp,
                                color = if (isSelected) Color.Red else Color.Transparent
                            ),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = piece,
                            color = if (piece in listOf("車", "馬", "象", "士", "將", "炮", "兵")) Color.Black else Color.Red,
                            fontSize = 24.sp,
                            textAlign = TextAlign.Center
                        )
                    }
                }
            }
        }
    }
}

private fun initChessBoard(): List<List<String>> {
    return listOf(
        listOf("車", "馬", "象", "士", "將", "士", "象", "馬", "車"),
        listOf("", "炮", "", "", "", "", "", "炮", ""),
        listOf("兵", "", "兵", "", "兵", "", "兵", "", "兵"),
        List(9) { "" },
        List(9) { "" },
        List(9) { "" },
        List(9) { "" },
        listOf("卒", "", "卒", "", "卒", "", "卒", "", "卒"),
        listOf("", "砲", "", "", "", "", "", "砲", ""),
        listOf("車", "馬", "相", "仕", "帥", "仕", "相", "馬", "車")
    )
}

private fun movePiece(
    board: List<List<String>>,
    from: Pair<Int, Int>,
    to: Pair<Int, Int>
): List<List<String>> {
    if (from.first !in 0..8 || from.second !in 0..9 || to.first !in 0..8 || to.second !in 0..9) {
        return board
    }
    val newBoard = board.map { it.toMutableList() }.toMutableList()
    newBoard[to.second][to.first] = newBoard[from.second][from.first]
    newBoard[from.second][from.first] = ""
    return newBoard
}